package com.kerbores.blog.service.impl;

import java.util.Date;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import com.kerbores.blog.bean.Article;
import com.kerbores.blog.constant.Constant;
import com.kerbores.blog.dao.IArticleDao;
import com.kerbores.blog.dao.ITagDao;
import com.kerbores.blog.service.IArticleService;
import com.kerbores.utils.common.DateUtils;
import com.kerbores.utils.web.pager.Pager;

/**
 * @author 贵源 <br>
 *         create at 2014年10月21日
 */
@IocBean(name = "articleService")
public class ArticleServiceImpl implements IArticleService {
	@Inject
	private IArticleDao articleDao;
	@Inject
	private ITagDao tagDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.service.IArticleService#findByPage(int)
	 */
	@Override
	public Pager<Article> findByPage(int page) {
		Pager<Article> pager = new Pager<Article>();
		pager.setPage(page);
		pager.setEntities(articleDao.searchByPage(null, page, Constant.DEFAULT_PAGE_SIZE));
		pager.setCount(articleDao.countAll());
		return pager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.service.IArticleService#findByCatalog(int, int)
	 */
	@Override
	public Pager<Article> findByCatalog(int id, int page) {
		Condition cnd = Cnd.where("catalogId", "=", id);
		Pager<Article> pager = new Pager<Article>();
		pager.setPage(page);
		pager.setEntities(articleDao.searchByPage(cnd, page, Constant.DEFAULT_PAGE_SIZE));
		pager.setCount(articleDao.countByCnd(cnd));
		return pager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kerbores.blog.service.IArticleService#save(com.kerbores.blog.bean
	 * .Article)
	 */
	@Override
	public boolean save(Article article) {
		article.setTagInfo(article.getTagInfo().indexOf(",") > 0 ? article.getTagInfo() : article.getTagInfo() + ",");
		return articleDao.save(article) != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.service.IArticleService#get(int)
	 */
	@Override
	public Article get(int id) {
		Article article = articleDao.findById(id);
		if (article.getTagInfo() != null && !Strings.isBlank(article.getTagInfo().trim())) {
			article.setTags(tagDao.findByIds(article.getTagInfo()));
		}
		article.setViewCount(article.getViewCount() + 1);// 刷次数
		articleDao.update(article);
		return article;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kerbores.blog.service.IArticleService#listByDateAndPage(java.util
	 * .Date, int)
	 */
	@Override
	public Pager<Article> listByDateAndPage(Date date, int page) {
		Pager<Article> pager = new Pager<Article>();
		pager.setPage(page);
		Condition cnd = Cnd.where("publishTime", ">", date).and("publishTime", "<", DateUtils.addDays(date, 1));
		pager.setEntities(articleDao.searchByPage(cnd, page, Constant.DEFAULT_PAGE_SIZE));
		pager.setCount(articleDao.countByCnd(cnd));
		return pager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.service.IArticleService#search(java.lang.String,
	 * int)
	 */
	@Override
	public Pager<Article> search(String key, int page) {
		Pager<Article> pager = new Pager<Article>();
		pager.setPage(page);
		Condition cnd = Cnd.where("title", "like", "%" + key + "%").or("content", "like", "%" + key + "%").or("summary", "like", "%" + key + "%");
		pager.setEntities(articleDao.searchByPage(cnd, page, Constant.DEFAULT_PAGE_SIZE));
		pager.setCount(articleDao.countByCnd(cnd));
		return pager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.service.IArticleService#findByTagPage(int, int)
	 */
	@Override
	public Pager<Article> findByTagPage(int id, int page) {
		Pager<Article> pager = new Pager<Article>();
		pager.setPage(page);
		Condition cnd = Cnd.where("tagInfo", "like", "%" + id + ",%");
		pager.setEntities(articleDao.searchByPage(cnd, page, Constant.DEFAULT_PAGE_SIZE));
		pager.setCount(articleDao.countByCnd(cnd));
		return pager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.service.IArticleService#loadNew()
	 */
	@Override
	public List<Article> loadNew() {
		return articleDao.searchByPage(Cnd.orderBy().desc("id"), 1, 10);
	}

}
